package cn.project.lemontree.controller;

import cn.project.lemontree.bean.ParentChild;
import cn.project.lemontree.service.ParentChildService;
import cn.project.lemontree.tools.CommonResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/pc")
@Api(tags = "亲子关系管理模块")
@CrossOrigin  //跨域访问
public class ParentChildController {
    @Resource
    private ParentChildService parentChildService;

    @ApiOperation("分页查询亲子关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="页码",required=true,dataTypeClass = int.class,example = "1"),
            @ApiImplicitParam(name="size",value="每页行数",required=true,dataTypeClass = int.class,example = "10"),
            @ApiImplicitParam(name="parentName",value="家长名称",required=false,dataTypeClass = String.class),
            @ApiImplicitParam(name="childName",value="儿童名称",required=false,dataTypeClass = String.class)
    })
    @GetMapping("/listpage")
    public CommonResult listpage(int page,int size,String childName,String parentName){
        Map<String,Object> map = new HashMap<>();
        map.put("count",parentChildService.count(childName,parentName));
        map.put("data",parentChildService.listpage(page,size,childName,parentName));
        return CommonResult.success(map);
    }

    @ApiOperation("删除亲子关系")
    @GetMapping("/remove")
    public CommonResult remove(Integer childId,Integer parentId){
        return CommonResult.success(parentChildService.remove(childId,parentId));
    }

    @ApiOperation("根据儿童查询家长")
    @GetMapping("/findbychild")
    public CommonResult findByChild(Integer childId,String childName){
        return CommonResult.success(parentChildService.findAllByChild(childId,childName));
    }

    @ApiOperation("根据家长查询儿童")
    @GetMapping("/findbyparent")
    public CommonResult findByParent(Integer parentId,String parentName){
        return CommonResult.success(parentChildService.findAllByParent(parentId,parentName));
    }

    @ApiOperation("添加亲子关系")
    @PostMapping("/save")
    public CommonResult save(@RequestBody ParentChild parentChild){
        return CommonResult.success(parentChildService.save(parentChild));
    }
}
